/*
 * Copyright (C) 2020 Tencent Cloud.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import uploadFile from './upload-file';
import getFileAccessUrl from './getFileAccessUrl';

/**
 * 选择并上传图片到腾讯云COS（需要调用云函数签名，请先配置好云函数）
 * @async
 * @return {Promise<string>} 返回成功上传到COS上的文件名称
 */
export default async function chooseAndUploadImage(count = 1, artPptNum) {
	let urls = []
	let [error, res] = await uni.chooseImage({
		count: count,
		sizeType: "compressed",
	});
	if (error) {
		throw error;
	}

	console.log(res)
	// #ifdef H5
	if (!/^image/.test(res.tempFiles[0].type)) {
		throw new Error('文件类型错误');
	}
	// #endif

	try {
		let files = res.tempFilePaths;
		// #ifdef H5
		files = res.tempFiles;
		// #endif
		for (let file of files) {
			let url = await uploadFile(file, file.name, "images", artPptNum);
			//console.log(url)
			//let url = await getFileAccessUrl(key);
			urls.push('https://' + url)
		}
		return urls;
	} catch (error) {
		throw error;
	} finally {
		uni.hideLoading();
	}
};